package com.simplecontact.activities;

import java.io.IOException;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.simplecontact.R;
import com.simplecontact.classes.Contact;
import com.simplecontact.classes.ContactBundle;
import com.simplecontact.utils.Utils;

public class ActCreateContact extends Activity {
			
	ContactBundle cb = null;
	EditText edNickName;
	EditText edFirstName;
	EditText edLastName;
	EditText edEmail;
	EditText edPhoneNumber;
	EditText edAddress;
	ImageView ivImage;
	Button btnCapture;	
	Button btnDone;
	Button btnRevert;
	private String strImage;
	
	public ContentValues prepareToInsert(){
		ContentValues values = new ContentValues();
		values.put(Contact.ContactColumns.NICKNAME, edNickName.getText().toString());
		values.put(Contact.ContactColumns.FIRSTNAME, edFirstName.getText().toString());
		values.put(Contact.ContactColumns.LASTNAME, edLastName.getText().toString());
		values.put(Contact.ContactColumns.EMAIL, edEmail.getText().toString());
		values.put(Contact.ContactColumns.PHONENUMBER, edPhoneNumber.getText().toString());
		if(strImage=="" || strImage==null)values.put(Contact.ContactColumns.IMAGE, "");
		else values.put(Contact.ContactColumns.IMAGE, strImage);
		values.put(Contact.ContactColumns.ADDRESS, edAddress.getText().toString());
		return values;
	}
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_contact);
        edNickName = (EditText)findViewById(R.id.edNickName);
        edFirstName = (EditText)findViewById(R.id.edFirstName);
        edLastName = (EditText)findViewById(R.id.edLastName);
        edEmail = (EditText)findViewById(R.id.edEmail);
        edPhoneNumber = (EditText)findViewById(R.id.edPhoneNumber);
        edAddress = (EditText)findViewById(R.id.edAddress);
        ivImage = (ImageView)findViewById(R.id.ivimage);
        btnDone = (Button)findViewById(R.id.btnDone);
        btnDone.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(cb==null)getContentResolver().insert(Contact.ContactColumns.CONTENT_URI, prepareToInsert());
    			else getContentResolver().update(Contact.ContactColumns.CONTENT_URI, prepareToInsert(), "_id=" + cb.getId(), null);
    			finish();
			}
		});
        
        btnRevert = (Button)findViewById(R.id.btnRevert);
        btnRevert.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();    			
			}
		});
        
        btnCapture = (Button)findViewById(R.id.btnImage);
        btnCapture.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
	            intent.setType("image/*");
	            intent.setAction(Intent.ACTION_GET_CONTENT);
	            startActivityForResult(Intent.createChooser(intent, "Select Picture"), Utils.CAPTURE_IMAGE);				
			}
		});
        cb = getIntent().getParcelableExtra("contactbundle");        
        if(cb!=null){
        	edNickName.setText(cb.getNickName());
        	edFirstName.setText(cb.getFirstName());
        	edLastName.setText(cb.getLastName());
        	edEmail.setText(cb.getEmail());
        	edPhoneNumber.setText(cb.getPhoneNumber());
        	edAddress.setText(cb.getAddress());
        	//Chuyen cb.getImage() thanh mang byte, sau do setBitmap cho ivImage
        	if(cb.getImage().length()!=0){
	        	byte[] i;
				try {
					i = com.simplecontact.utils.Base64.decode(cb.getImage());
					ivImage.setImageBitmap(BitmapFactory.decodeByteArray(i, 0, i.length));
				} catch (IOException e) { e.printStackTrace(); }        	        	
        	}
        	else{
        		ivImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.user));
        	}
        }
    }
    
    @Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Utils.CAPTURE_IMAGE) {
            if (resultCode == RESULT_OK) {
            	//Save image to PictureAnswer object
            	if(data!=null){
            		Uri selectedImageUri = data.getData();
                    //OI FILE Manager
                    String filemanagerstring = selectedImageUri.getPath();
                    if(filemanagerstring!="")
                    	ivImage.setImageURI(selectedImageUri);
            	}
            }
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {      
    	menu.add(0, Utils.MENU_DONE, 0, getString(R.string.strDone)).setIcon(R.drawable.update);
    	menu.add(0, Utils.MENU_REVERT, 0,getString(R.string.strRevert)).setIcon(R.drawable.cancel);
    	return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {    
    	// Handle item selection    
    	switch (item.getItemId()) {  
    		case Utils.MENU_DONE:
    			if(cb==null)getContentResolver().insert(Contact.ContactColumns.CONTENT_URI, prepareToInsert());
    			else getContentResolver().update(Contact.ContactColumns.CONTENT_URI, prepareToInsert(), "_id=" + cb.getId(), null);
    			finish();
    			break;
    		case Utils.MENU_REVERT:
    			finish();
    			break;               		
    	}    	
    	return super.onOptionsItemSelected(item);
    }
}
